package com.frxuexi.park2212.patrol

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.TimeUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.DateBean
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityTaskBinding
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@Route(path = "/app/TaskActivity")
@AndroidEntryPoint
class TaskActivity : BaseActivity<ActivityTaskBinding,PatrolViewModel>() {
    val dateAdapter by lazy { DateAdapter() }
    val taskAdapter by lazy { TaskAdapter() }

    override fun initData() {
        vm.getTasksByStatus.observe(this){
            taskAdapter.data.clear()
            taskAdapter.addData(it)
            taskAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
//        周几 week  start
//        周日  1      0
//        周一  2      -1
//        周二  3      -2
//        周三  4      -3
        var calendar = Calendar.getInstance()
        val week = calendar.get(Calendar.DAY_OF_WEEK)

        val list = mutableListOf<DateBean>()
        for (i in -(week-1)..-(week-1)+6){
            calendar = Calendar.getInstance()
            calendar.add(Calendar.DAY_OF_MONTH,i)
            val day = calendar.get(Calendar.DAY_OF_MONTH)
            val week1 = TimeUtils.getChineseWeek(calendar.time).substring(1,2)
            list.add(DateBean(week1,day,if(i==0) true else false))
        }

        Log.i("===",list.toString())
        dateAdapter.addData(list)
        binding.taskRvdate.apply {
            adapter=dateAdapter
            layoutManager=GridLayoutManager(this@TaskActivity,7)
        }

        binding.taskRv.apply {
            adapter=taskAdapter
            layoutManager=LinearLayoutManager(this@TaskActivity)
        }

        taskAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/TaskDetailActivity").withString("tid",taskAdapter.data.get(position).id.toString()).navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getTasksByStatus("0")
    }
}